package com.yd.mvc.pc.goodsTypeController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.gtypeService.GTypeService;
import com.yd.domain.business.GType;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.GTypeParam;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-08 17:12
 * @desc 商品类型维护
 **/
@RestController
@RequestMapping(value="/api/gType")
public class GoodsTypeController extends BaseController{
    @Autowired
    private GTypeService gTypeService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 录入商品类型
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addGType(@RequestBody GType gType,HttpServletRequest request)  {
        String user="admin";
        String suff="ID:"+gType.getId()+";名称:"+gType.getTypeName()+";价格:"+gType.getPrice()+";启用:"+gType.isIfUse()+";新旧物："+gType.getGoodKind();
        try {
            gTypeService.addGType(gType);
            loggerService.addLogger(new Logger("新增商品类型记录",user,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增商品类型记录",user,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 修改商品类型信息
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateGType(@RequestBody GType gType,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+gType.getId()+";名称:"+gType.getTypeName()+";价格:"+gType.getPrice()+";启用:"+gType.isIfUse()+";新旧物："+gType.getGoodKind();
        try {
            gTypeService.updateGType(gType);
            loggerService.addLogger(new Logger("修改商品类型记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改商品类型记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 删除商品类型
     * @Date: 2018/3/9
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteGType(@PathVariable("id") String id,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+id;
        try {
            gTypeService.deleteGType(id);
            loggerService.addLogger(new Logger("删除商品类型记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("删除商品类型记录",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 停用、启用商品类型
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/updateUse", method = RequestMethod.PUT)
    public void updateUse(@RequestBody GType gType,HttpServletRequest request){
        String user="admin";
        String suff="ID:"+gType.getId()+";启用:"+gType.isIfUse();
        try {
            gTypeService.updateUse(gType);
            loggerService.addLogger(new Logger("启用、停用商品类型",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("启用、停用商品类型",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 分页查询数据列表
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getGTypeList(@RequestBody GTypeParam gTypeParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=gTypeService.getGTypeList(gTypeParam,pageIndex,pageSize);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc: 分页查询数据列表
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/GTypelist/{type}", method = RequestMethod.GET)
    public List<GType> getGTypeList(@PathVariable("type")String type){
        List<GType> list=gTypeService.getGTypeList(type);
        return list;
    }
    /**
     * @Author: zy
     * @Desc: 批量停用、启用
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/updateAllIfUse",method = RequestMethod.POST)
    public void updateAllIfUse(@RequestBody GType gType,HttpServletRequest request){
        String user="admin";
        String suff="批量停用、启用ID:"+gType.getStr();
        try {
            String str=gType.getStr();
            String strN= str.substring(0,str.length()-1);//去除 "，"
            gType.setStr(strN);
            gTypeService.updateAllIfUse(gType);
            loggerService.addLogger(new Logger("批量启用、停用商品类型",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        }catch (Exception e){
            loggerService.addLogger(new Logger("批量启用、停用商品类型",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }

    }
    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/9
     * @Param
     */
    @RequestMapping(value = "/deleteAll",method = RequestMethod.POST)
    public void deleteAll(@RequestBody GType gType,HttpServletRequest request){
        String user="admin";
        String suff="批量删除ID:"+gType.getStr();
        try {
            String str=gType.getStr();
            String strN= str.substring(0,str.length()-1);//去除 "，"
            gType.setStr(strN);
            gTypeService.deleteAll(gType);
            loggerService.addLogger(new Logger("批量删除商品类型",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
        }catch (Exception e){
            loggerService.addLogger(new Logger("批量删除商品类型",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.GOODTYPE,"",suff),request);
            e.printStackTrace();
        }
    }

    /**
     * 一键更新单价
     * @param gType
     * @param request
     */
    @RequestMapping(value = "/update/price",method = RequestMethod.PUT)
    public void updatePrice(@RequestBody GType gType,HttpServletRequest request){
        gTypeService.updatePrice(gType);

    }

    /*@RequestMapping(value = "/todayPrice",method = RequestMethod.PUT)
    public void getTodayPrice(@RequestBody String gTypeId){
        gTypeService.getTodayPrice(gType);

    }*/

}
